/*
 * Copyright 2008 FBK (http://www.fbk.eu/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fbk.it.hlt.jlsi.util;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * to do
 * 
 * @author Claudio Giuliano
 * @version 1.0
 * @since 1.0
 * @see Filter
 * @see FilterFactory
 */
public class WordSet {
	/**
	 * Define a static logger variable so that it references the Logger instance
	 * named <code>WordSet</code>.
	 */
	static Logger logger = Logger.getLogger(WordSet.class.getName());

	//
	private Set set;

	//
	public WordSet() {
		set = new HashSet();
	} // end constructor

	//
	public boolean contains(String w) {
		return set.contains(w.toLowerCase());
	} // end contains

	//
	public int size() {
		return set.size();
	} // end size

	public void read(Reader in) throws IOException {
		logger.info("reading word set...");

		LineNumberReader lnr = new LineNumberReader(in);

		String line;
		String[] s;
		// Integer id;
		while ((line = lnr.readLine()) != null) {
			line = line.trim();
			if (!line.startsWith("#") && line.length() > 0) {
				set.add(line.toLowerCase().trim());
			}
		}
		lnr.close();
		in.close();

	} // end read

	//
	public void write(Writer writer) throws IOException {
		PrintWriter pw = new PrintWriter(writer);

		// iterates over the training set
		Iterator it = set.iterator();
		while (it.hasNext()) {
			String s = (String) it.next();
			pw.println(s);
			pw.flush();

		} // end while
		pw.close();

	} // end write

	//
	public String toString() {
		StringBuffer sb = new StringBuffer();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			String s = (String) it.next();
			sb.append(s);
			sb.append("\n");

		} // end while

		return sb.toString();
	} // end toString

	//
	public static void main(String[] args) throws Exception {
		String logConfig = System.getProperty("log-config");
		if (logConfig == null)
			logConfig = "log-config.txt";

		long begin = System.currentTimeMillis();

		PropertyConfigurator.configure(logConfig);

		if (args.length != 1) {
			System.out
					.println("Usage: java -mx1024M org.fbk.it.hlt.jlsi.util.WordSet file");
			System.exit(1);
		}

		WordSet set = new WordSet();
		set.read(new FileReader(args[0]));
		logger.info("set:\n" + set.toString());
	} // end main

} // end class WordSet